package com.haibara.codesubmiteval.chain.userChain;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 构造责任链
 *
 * @Author: mengyu
 * @Date: 2025/4/28
 */
@Configuration
public class CreateUserDefaultValueChainBuilder {
    @Autowired
    private List<CreateUserDefaultValueHandler> handlers;

    private CreateUserDefaultValueHandler chainHead;

    @PostConstruct
    public void initChain() {
        // 构建责任链
        for (int i = 0; i < handlers.size() - 1; i++) {
            handlers.get(i).setNext(handlers.get(i + 1));
        }
        // 保存链头
        if (!handlers.isEmpty()) {
            chainHead = handlers.get(0);
        }
    }

    @Bean
    public CreateUserDefaultValueHandler createUserDefaultValueHandlerHeader() {
        return chainHead;
    }
}
